<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head th:include="include :: header"></head>
<body class="hold-transition skin-blue sidebar-mini">

<!--库存明细信息模态框-->
<div class="modal fade" id="modal-chooseStock" tabindex="-1" data-keyboard="true">
    <div class="modal-dialog" style="width: 1350px;">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">在库明细信息</h4>
            </div>
            <div class="modal-body">
                <div class="row">
                    <div class="col-xs-12">
                        <div class="box">
                            <div class="box-header">
                                <div class="row" id="HiddenDiv" style="margin-bottom: 10px">
                                    <div class="form-inline">
                                        <div class="form-group" style="margin-right: 10px;">
                                            <label>
                                                原料类型
                                            </label>
                                            <select id="materialKindQuery" name="materialKindQuery"
                                                    class="form-control select2">
                                                <option value="型材">型材</option>
                                                <option value="板材">板材</option>
                                                <option value="" selected="selected">全部</option>
                                            </select>
                                        </div>
                                        <div class="form-group" style="margin-right: 10px;">
                                            <label>项目编号</label>
                                            <input type="text" id="projectCodeQuery" name="projectCodeQuery"
                                                   class="form-control"
                                                   placeholder="请输入项目编号">
                                        </div>
                                        <div class="form-group" style="margin-right: 10px;">
                                            <label>项目名称</label>
                                            <input type="text" id="projectNameQuery" name="projectNameQuery"
                                                   class="form-control"
                                                   placeholder="请输入项目名称">
                                        </div>
                                        <div class="form-group" style="margin-right: 10px;">
                                            <label>跟踪号</label>
                                            <input type="text" id="trackCodeQuery" name="trackCodeQuery"
                                                   class="form-control"
                                                   placeholder="请输入跟踪号">
                                        </div>
                                    </div>
                                </div>
                                <div class="row">
                                    <div class="form-inline">
                                        <div class="form-group" style="margin-right: 10px;">
                                            <label>
                                                在库类型
                                            </label>
                                            <select id="inTypeQuery" name="inTypeQuery" class="form-control select2">
                                                <option value="1">库存在库</option>
                                                <option value="2">项目在库</option>
                                                <option value="" selected="selected">全部</option>
                                            </select>
                                        </div>
                                        <div class="form-group" style="margin-right: 10px;">
                                            <label>原料类型</label>
                                            <input type="text" id="stockMaterialTypeQuery" name="stockMaterialTypeQuery"
                                                   class="form-control"
                                                   placeholder="请输入原料类型">
                                        </div>
                                        <div class="form-group" style="margin-right: 10px;">
                                            <label>原料材质</label>
                                            <input type="text" id="stockMaterialQualityQuery"
                                                   name="stockMaterialQualityQuery" class="form-control"
                                                   placeholder="请输入原料材质">
                                        </div>
                                        <div class="form-group" style="margin-right: 10px;">
                                            <label>原料规格</label>
                                            <input type="text" id="stockMaterialSpecQuery"
                                                   name="stockMaterialSpecQuery" class="form-control"
                                                   placeholder="请输入原料规格">
                                        </div>
                                        <button class="btn btn-vk" id="btn_stockSearch">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;搜索&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</button>
                                    </div>
                                </div>
                            </div>
                            <div class="box-body">
                                <table id="stockQueryTable" class="table table-bordered" width="100%">
                                    <thead>
                                    <tr>
                                        <th>序号</th>
                                        <th>分类</th>
                                        <th>原料描述</th>
                                        <th>在库数量</th>
                                        <th>可用数量</th>
                                        <th>在库重量</th>
                                        <th>可用重量</th>
                                        <th>跟踪号</th>
                                        <th>项目编码</th>
                                        <th>项目名称</th>
                                        <th>在库类型</th>
                                        <th>特殊说明</th>
                                        <th>选择</th>
                                    </tr>
                                    </thead>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <!-- /.modal-content -->
    </div>
    <!-- /.modal-dialog -->
</div>

<section class="content-header">
    <h1>
        还料操作<span th:text="${status}"></span>
    </h1>
</section>
<section class="content">
    <div class="row">
        <div class="col-md-6">
            <div class="box">
                <div class="box-header">
                    <div class="col-md-12">
                        <h3>串料出库基本信息</h3>
                    </div>
                </div>
                <div class="box-body">
                    <div class="row">
                        <div class="col-md-12">
                            <form id="lendForm" method="post" class="form-horizontal" role="form">
                                <div class="form-group">
                                    <label class="col-sm-2 text-right">串料申请单号</label>
                                    <div class="col-sm-10">
                                        <span th:text="${lend.lendCode}"></span>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-2 text-right">需求项目</label>
                                    <div class="col-sm-10">
                                        <span th:text="${lend.needProCode + '&nbsp&nbsp&nbsp&nbsp' + lend.needProName}"></span>
                                    </div>
                                </div>
                                <!--<div class="form-group">
                                    <label class="col-sm-2 text-right">需求项目编号</label>
                                    <div class="col-sm-10">
                                        <span th:text="${lend.needProCode}"></span>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-2 text-right">需求项目名称</label>
                                    <div class="col-sm-10">
                                        <span th:text="${lend.needProName}"></span>
                                    </div>
                                </div>-->
                                <div class="form-group">
                                    <label class="col-sm-2 text-right">计划归还编码</label>
                                    <div class="col-sm-10">
                                        <span th:text="${lend.needTrackCode}"></span>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-2 text-right">需求物料描述</label>
                                    <div class="col-sm-10">
                                        <span th:text="${lend.needMaterialDesc}"></span>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-2 text-right">需求数量</label>
                                    <div class="col-sm-4">
                                        <span th:text="${lend.needQty+lend.needMaterialBase.numUnit}"></span>
                                    </div>
                                    <label class="col-sm-2 text-right">需求重量</label>
                                    <div class="col-sm-4">
                                        <span th:text="${lend.needWgt+lend.needMaterialBase.weightUnit}"></span>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-2 text-right">使用部门</label>
                                    <div class="col-sm-10">
                                        <span th:text="${lend.needDepartment}"></span>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-2 text-right">挪用项目</label>
                                    <div class="col-sm-10">
                                        <span th:text="${lend.lendProCode+ '&nbsp&nbsp&nbsp&nbsp' +lend.lendProName}"></span>
                                    </div>
                                </div>
                                <!--<div class="form-group">
                                    <label class="col-sm-2 text-right">挪用项目编号</label>
                                    <div class="col-sm-10">
                                        <span th:text="${lend.lendProCode}"></span>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-2 text-right">挪用项目名称</label>
                                    <div class="col-sm-10">
                                        <span th:text="${lend.lendProName}"></span>
                                    </div>
                                </div>-->
                                <div class="form-group">
                                    <label class="col-sm-2 text-right">挪用计划编码</label>
                                    <div class="col-sm-10">
                                        <span th:text="${lend.lendTrackCode}"></span>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-2 text-right">挪用物料描述</label>
                                    <div class="col-sm-10">
                                        <span th:text="${lend.lendMaterialDesc}"></span>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-2 text-right">实际挪用数量</label>
                                    <div class="col-sm-4">
                                        <span th:text="${lend.outQty+lend.needMaterialBase.numUnit}"></span>
                                    </div>
                                    <label class="col-sm-2 text-right">实际挪用重量</label>
                                    <div class="col-sm-4">
                                        <span th:text="${lend.outWgt+lend.needMaterialBase.weightUnit}"></span>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-2 text-right">是否需要还料</label>
                                    <div class="col-sm-4">
                                        <span th:text="${lend.returnFlg == 1}?'需要还':'无需还'"></span>
                                    </div>
                                    <div class="col-sm-2">
                                        <button type="button" id="btn_returnflg" class="btn btn-info pull-right">还料变更
                                        </button>
                                    </div>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-md-6">
            <form id="returnMaterialForm" th:action="@{/mm/returnmaterial/create}" autocomplete="off" method="post"
                  class="form-horizontal" role="form">
                <input type="hidden" name="lendLog.id" th:value="${lend.id}">
                <div class="box">
                    <div class="box-header">
                        <div class="col-md-12">
                            <h3>还料操作</h3>
                        </div>
                    </div>
                    <div class="box-body">
                        <div class="row">
                            <div class="col-md-12">
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">项目编号</label>
                                    <input type="hidden" name="materialStockDetail.id" id="materialStockDetailId">
                                    <input type="hidden" id="materialSpec">
                                    <input type="hidden" id="materialLength">
                                    <input type="hidden" id="materialWidth">
                                    <input type="hidden" id="materialHeight">
                                    <input type="hidden" id="formulaType">
                                    <div class="col-sm-8">
                                        <input type="text" readonly="readonly" class="form-control" name="returnProCode"
                                               id="returnProCode" placeholder="还料项目编号">
                                    </div>
                                    <div class="col-sm-2">
                                        <button type="button" class="btn btn-info pull-right"
                                                id="btn-chooseStock">
                                            <span class="glyphicon glyphicon-search"></span>选 择
                                        </button>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">项目名称</label>
                                    <div class="col-sm-10">
                                        <input type="text" readonly="readonly" class="form-control" name="returnProName"
                                               id="returnProName" placeholder="还料项目名称">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">计划编码</label>
                                    <div class="col-sm-10">
                                        <input type="text" readonly="readonly" class="form-control"
                                               name="returnTrackCode"
                                               id="returnTrackCode" placeholder="还料计划编码">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">物料描述</label>
                                    <div class="col-sm-10">
                                        <input type="text" readonly="readonly" class="form-control" name="materialDesc"
                                               id="materialDesc" placeholder="还料物料描述">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <input type="hidden" id="hiddenQty" th:value="${lend.outQty}">
                                    <input type="hidden" id="hiddenWgt" th:value="${lend.outWgt}">
                                    <label class="col-sm-2 control-label">数量</label>
                                    <div class="col-sm-2" style="padding-right: 0px;">
                                        <input type="number" step="0.1" style="padding-right: 5px;"
                                               class="form-control" id="returnQty" name="returnQty"
                                               autocomplete="off" placeholder="数量">
                                    </div>
                                    <label class="col-sm-1 control-label" id="numUnit"></label>
                                    <label class="col-sm-2 control-label">重量</label>
                                    <div class="col-sm-2" style="padding-right: 0px;">
                                        <input type="number" step="0.1" style="padding-right: 5px;"
                                               class="form-control" id="returnWgt" name="returnWgt"
                                               autocomplete="off" placeholder="重量">
                                    </div>
                                    <label class="col-sm-1 control-label" id="weightUnit"></label>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">备注</label>
                                    <div class="col-sm-10">
                                        <input type="text" class="form-control" id="remark" name="remark"
                                               placeholder="备注">
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="box-footer">
                        <button type="button" id="btn_create" class="btn btn-info pull-right"
                                th:style="'display:' + @{(${lend.returnFlg}==1 ? 'inline-block' : 'none')} + ''">提交
                        </button>
                        <button type="button" id="btn_updateStatus" class="btn btn-warning pull-right"
                                th:style="'display:' + @{(${lend.returnFlg}==1 ? 'inline-block' : 'none')} + ';margin-right: 10px;'">
                            还料状态
                        </button>
                    </div>
                </div>
            </form>
        </div>
    </div>
    <table id="returnMaterialTable" class="table table-bordered table-hover" width="100%">
        <thead>
        <tr>
            <th>序号</th>
            <th>项目编号</th>
            <th>项目名称</th>
            <th>计划编码</th>
            <th>物料描述</th>
            <th>数量</th>
            <th>单位</th>
            <th>重量</th>
            <th>单位</th>
            <th>备注</th>
            <th>还料日期</th>
            <th>还料人</th>
            <th>操作</th>
        </tr>
        </thead>
    </table>
</section>
<div th:include="include :: footer"></div>

<script>

    function saveReturnMaterial() {
        if ($.validate.isValid()) {
            $.operate.post(ctx + "mm/returnmaterial/create", $("#returnMaterialForm").serialize(), function (result) {

                if (result.content.length == 0) {
                    location.reload();
                } else {
                    alert(result.content);
                }
                // $.table.refresh();
                // $.validate.reset();
            }, "application/x-www-form-urlencoded", true)
        }
    }

    // 删除
    function updateStatus(id) {
        $.modal.confirm("确定删除这个还料吗?", function () {
            $.operate.post(ctx + "mm/returnmaterial/del", {"id": id}, function () {
                location.reload();
                // $.table.refresh();
            });
        });
    }

    // 取得在库明细行数据
    function getStock(c) {

        $("#materialStockDetailId").val(c.id);
        $("#returnProCode").val(c.projectCode);
        $("#returnProName").val(c.projectName);
        $("#returnTrackCode").val(c.trackCode);
        $("#numUnit").text(c.numUnit);
        $("#weightUnit").text(c.weightUnit);

        $("#returnQty").val($("#hiddenQty").val());
        $("#returnWgt").val($("#hiddenWgt").val());

        $("#materialSpec").val(c.materialSpec);
        $("#materialLength").val(c.materialLength);
        $("#materialWidth").val(c.materialWidth);
        $("#materialHeight").val(c.materialHeight);
        $("#formulaType").val(c.formulaType);

        var lendMaterialDesc = c.materialType + '_' + c.materialQuality + '_' + c.materialSpec + '   ' + c.materialWidth + '*' + c.materialLength + '*' + c.materialHeight;
        $("#materialDesc").val(lendMaterialDesc.replace('null', ' '));

        // 关闭模态框
        $.modal.hide("modal-chooseStock");
    }

    $(function () {

        //打开库存明细模态框
        $("#btn-chooseStock").click(function () {
            $.modal.open("modal-chooseStock");
        });

        $.modal.openEvent("modal-chooseStock", function () {
            // 加载列表
            $.doubleTable.init({
                id: "stockQueryTable",
                info: true,
                fixedHeader: true,
                scrollY: '500px',
                dom: "t",
                url: ctx + 'materialrequest' + "/lend/stock",
                columns: [
                    {
                        data: null,
                        width: "40px",
                        render: function (data, type, row, meta) {
                            return meta.row + 1;
                        }
                    },
                    {
                        data: "materialKind",
                        width: "80px"
                    },
                    {
                        data: "materialNameDescript"
                    },
                    {
                        data: "totalNum"
                    },
                    {
                        data: "enableNum"
                    },
                    {
                        data: "totalWeight"
                    },
                    {
                        data: "enableWeight"
                    },
                    {
                        data: "trackCode"
                    },
                    {
                        data: "projectCode"
                    },
                    {
                        data: "projectName"
                    },
                    {
                        data: "inTypeName"
                    },
                    {
                        data: "spec"
                    },
                    {
                        data: null,
                        render: function (data, type, row, meta) {
                            var html = [];
                            html.push("<a style='margin-right: 5px' class='btn btn-danger btn-xs intent-del' href='javascript:void(0);' onclick='getStock(" + JSON.stringify(row) + " )'>选择</a>");
                            return html.join('');
                        }
                    }
                ],
                createdRow: function (row, data, index) {
                    if (data.spec != null && data.spec.indexOf("不合格") >= 0) {
                        $(row).css('color', '#FF0000');
                    }
                },
            });
        });

        $.modal.hideEvent("modal-chooseStock", function () {
            $.doubleTable.destroy();
        });
        // 搜索
        $("#btn_stockSearch").click(function () {
            $.doubleTable.search({
                materialKindQuery: $("#materialKindQuery").val(),
                projectCodeQuery: $("#projectCodeQuery").val(),
                projectNameQuery: $("#projectNameQuery").val(),
                trackCodeQuery: $("#trackCodeQuery").val(),
                inTypeQuery: $("#inTypeQuery").val(),
                stockMaterialTypeQuery: $("#stockMaterialTypeQuery").val(),
                stockMaterialQualityQuery: $("#stockMaterialQualityQuery").val(),
                stockMaterialSpecQuery: $("#stockMaterialSpecQuery").val(),
            });
        });

        $("#returnQty").change(function () {

            var spec = $("#materialSpec").val();
            var length = $("#materialLength").val();
            var width = $("#materialWidth").val();
            var height = $("#materialHeight").val();
            var needQty = $("#returnQty").val();
            var formulaType = $("#formulaType").val();
            var totalWeight = returnMaterialWeight(spec, length, width, height, needQty, formulaType);
            $("#returnWgt").val(totalWeight);
        });

        // 还料保存
        $("#btn_create").click(function () {
            $.operate.post(ctx + "mm/returnmaterial/sum", {id: [[${lend.id}]]}, function (data) {
                if (parseInt($("#returnWgt").val()) + parseFloat(data.content) > [[${lend.outWgt}]]) {
                    $.modal.confirm("还料重量总和大于借料重量，是否继续?", function () {
                        saveReturnMaterial();
                    });
                } else {
                    saveReturnMaterial();
                }
            }, '', true);
        });

        // 是否还料状态变更
        $("#btn_returnflg").click(function () {
            $.operate.post(ctx + "mm/returnmaterial/updatereturnflg", {id: [[${lend.id}]]}, function (result) {

                if (result.content.length == 0) {
                    location.reload();
                } else {
                    alert(result.content);
                }
                // $.table.refresh();
                // $.validate.reset();
            }, "application/x-www-form-urlencoded", true)
        });

        // 还料状态变更 还完 未还完
        $("#btn_updateStatus").click(function () {
            $.modal.confirm("确定要更改还料状态吗？", function () {
                $.operate.post(ctx + "mm/returnmaterial/updateStatus", {id: [[${lend.id}]]}, function (result) {

                    if (result.content.length == 0) {
                        location.reload();
                    } else {
                        alert(result.content);
                    }
                    // $.table.refresh();
                    // $.validate.reset();
                }, "application/x-www-form-urlencoded", true)
            });
        });

        // 还料列表
        $.table.init({
            id: "returnMaterialTable",
            info: true,
            url: ctx + "mm/returnmaterial/project/list/",
            data: {
                id: [[${lend.id}]]
            },
            columns: [
                {
                    data: null,
                    render: function (data, type, row, meta) {
                        return meta.row + 1 + meta.settings._iDisplayStart;
                    }
                },
                {data: "materialStockDetail.projectCode"},
                {data: "materialStockDetail.projectName"},
                {data: "materialStockDetail.trackCode"},
                {data: "materialDesc"},
                {data: "returnQty"},
                {data: "materialStockDetail.materialBase.numUnit"},
                {data: "returnWgt"},
                {data: "materialStockDetail.materialBase.weightUnit"},
                {data: "remark"},
                {data: "operateDate"},
                {data: "operateUser"},
                {
                    data: "id",
                    render: function (data, type, row, meta) {
                        var html = [];
                        if (row.code == null) {
                            html.push('<a class="btn btn-danger btn-xs project-del" style="margin-left: 5px" href="javascript:void(0);" onclick="updateStatus(' + data + ')">删除</a>');
                        }
                        return html.join('');
                    }
                }
            ]
        })

        // 验证初始化
        $.validate.init("returnMaterialForm", {
            fields: {
                returnQty: {validators: {notEmpty: {message: '请输入还料数量'}}},
                returnWgt: {validators: {notEmpty: {message: '请输入还料重量'}}},
            }
        });

    });
</script>

</body>
</html>